﻿using AutoMapper;
using MediatR;
using SmartCity.ProcessEngine.Api.Read.Application.Command.Industry;
using SmartCity.ProcessEngine.Api.Read.Dtos;
using SmartCity.ProcessEngine.Domain.Entities;
using SmartCity.ProcessEngine.ErrorCode;
using SmartCity.ProcessEngine.Infrastructure.Interface;
using System.Collections.Generic;

namespace SmartCity.ProcessEngine.Api.Read.Application.CommandHandler.Industry
{
    public class GetIndustryHandler : IRequestHandler<GetIndustryCommand, ApiResult<List<IndustryDto>>>
    {
        private readonly IBaseRepository<IndustryModel> industryRepo;
        private readonly IMapper mapper;

        public GetIndustryHandler(IBaseRepository<IndustryModel> industryRepo,IMapper mapper)
        {
            this.industryRepo = industryRepo;
            this.mapper = mapper;
        }
        public async Task<ApiResult<List<IndustryDto>>> Handle(GetIndustryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<IndustryDto>> res = new ApiResult<List<IndustryDto>>();
            var ins = industryRepo.GetAll().Where(x=>!x.IsDeleted);
            var dtos = from i in ins
                       select new IndustryDto { 
                        Id = i.Id,
                           IndustryName = i.IndustryName
                       };

            res.Code = ApiEnum.Success;
            res.Msg = "查询行业列表成功";
            res.Data = dtos.ToList();
            return res;
        }
    }
}
